﻿package com.jxl.dnd.equipment
{
	
	public class Item
	{
		
		public function get id():uint { return __id; }
		
		public var name:String;
		public var cost:Number;
		public var weight:Number;
		public var preparationTime:Number; // in rounds
		public var itemEffect:ItemEffect;
		public var thumbnail:String;
		
		private var __id:uint;
		
		private static var idCounter:uint = 0;
		
		public function Item(name:String="", 
							  cost:Number=0, 
							  weight:Number=0, 
							  preparationTime:Number=0,
							  itemEffect:ItemEffect=null,
							  thumbnail:String=""):void
		{
			__id					= idCounter++;
			this.name 				= name;
			this.cost 				= cost;
			this.weight 			= weight;
			this.preparationTime 	= preparationTime;
			this.itemEffect 		= itemEffect;
			this.thumbnail 			= thumbnail;
		}
		
		public function toXML():XML
		{
			var itemXML:XML = 
				<item type="item">
					<id>{id}</id>
					<name>{name}</name>
					<cost>{cost}</cost>
					<weight>{weight}</weight>
					<preparationtime>{preparationTime}</preparationtime>
					<thumbnail>{thumbnail}</thumbnail>
				</item>;
			itemXML.appendChild(itemEffect.toXML());
			return itemXML;
		}
		
		public function buildFromXML(xml:XML):void
		{
			__id			= parseInt(xml.id);
			name			= xml.name;
			cost			= parseInt(xml.cost);
			weight			= parseInt(xml.weight);
			preparationTime	= parseInt(xml.preparationtime);
			// TODO: finish
			//itemEffect		= new ItemEffect();
			itemEffect.buildFromXML(new XML(xml..itemeffect[0]));
			thumbnail		= String(xml.thumbnail.text());
		}
	}
}